#include "settingdialog.h"
#include "appidsettingwgt.h"
#include "setting.h"

#include <QApplication>
#include <QBoxLayout>
#include <QPushButton>

SettingDialog::SettingDialog(QWidget* parent)
    : QDialog(parent)
    , m_appIdSettingWgt(new AppIdSettingWgt(this))
    , m_okBtn(new QPushButton(tr("Ok"), this))
    , m_cancelBtn(new QPushButton(tr("Cancel"), this))
{
    initUi();
    bindEvents();
}

void SettingDialog::initUi()
{
    AppIdSettingWgt::Data data;
    data.appId = SETTING.getAppId();
    data.secretKey = SETTING.getSecretKey();
    m_appIdSettingWgt->setData(data);

    setWindowTitle(QApplication::applicationName() + " - " + "Setting");
    m_okBtn->setMinimumWidth(50);
    m_cancelBtn->setMinimumWidth(50);

    QHBoxLayout* buttonsLayout = new QHBoxLayout();
    buttonsLayout->addStretch();
    buttonsLayout->addWidget(m_okBtn);
    buttonsLayout->addSpacing(20);
    buttonsLayout->addWidget(m_cancelBtn);

    QVBoxLayout* mainLayout = new QVBoxLayout(this);
    mainLayout->addWidget(m_appIdSettingWgt);
    mainLayout->addStretch();
    mainLayout->addLayout(buttonsLayout);
}

void SettingDialog::bindEvents()
{
    connect(m_okBtn, &QPushButton::clicked, this, [=]() {
        auto data = m_appIdSettingWgt->getData();
        SETTING.setAppId(data.appId);
        SETTING.setSecretKey(data.secretKey);
        accept();
    });

    connect(m_cancelBtn, &QPushButton::clicked, this, &QDialog::reject);
}
